package com.atguigu.meinian.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.meinian.constant.MessageConstant;
import com.atguigu.meinian.pojo.TravelItem;
import com.atguigu.meinian.service.TravelItemService;
import com.atguigu.meinian.vo.PageResult;
import com.atguigu.meinian.vo.QueryPageBean;
import com.atguigu.meinian.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


/**
 * @author zbystart
 * @create 2021-06-19 15:17
 */
@RestController
@RequestMapping("/travelitem")
public class TravelitemController {

    private Logger logger = LoggerFactory.getLogger(TravelitemController.class);

    @Reference
    private TravelItemService travelItemService;

    /**
     * 添加自由行数据
     * @param travelItem
     * @return
     */
    @PostMapping("/add.do")
    public R addTravelitem(@RequestBody TravelItem travelItem) {
        try {
            travelItemService.add(travelItem);
        } catch (Exception e) {
            logger.error("保存自游行失败{}",e);
            return R.error(MessageConstant.ADD_TRAVELITEM_FAIL);
        }

        return R.ok(MessageConstant.ADD_TRAVELITEM_SUCCESS,null);
    }


    @PostMapping("/getList.do")
    public PageResult getTravelitemList(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult =
                travelItemService.queryTravelitemList(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return pageResult;
    }

    @GetMapping("/show")
    public R show(Integer id) {
        try {
            TravelItem travelItem =  travelItemService.show(id);
            return R.ok(MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItem);
        } catch (Exception e) {
            return R.error(MessageConstant.QUERY_TRAVELITEM_FAIL);
        }
    }

    @PostMapping("/update")
    public R update(@RequestBody TravelItem travelItem) {
        try {
            travelItemService.update(travelItem);
            return R.ok(MessageConstant.EDIT_TRAVELITEM_SUCCESS,null);
        } catch (Exception e) {
            return R.error(MessageConstant.EDIT_TRAVELITEM_FAIL);
        }
    }

    @GetMapping("/delete")
    public R delete(Integer id) {
        try {
            travelItemService.delete(id);
            return R.ok(MessageConstant.DELETE_TRAVELITEM_SUCCESS,null);
        } catch (RuntimeException e) {
//            System.out.println(e.getClass().getName());
//            String msg = null;
//            if (e instanceof MeiNainTraveLitemException) {
//                MeiNainTraveLitemException e1 = (MeiNainTraveLitemException)e;
//                msg = e1.getMsg();
//            } else {
//                msg = e.getMessage();
//            }
            return R.error(e.getMessage());
        } catch (Exception e) {
            return R.error(MessageConstant.DELETE_TRAVELITEM_FAIL);
        }
    }

}
